package com.cwz.taculture.controller.register;

import com.cwz.taculture.service.register.RegisterService;
import com.cwz.taculture.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
@CrossOrigin
public class RegisterController {


    String code_a;

    @Autowired
    private RegisterService register_service;

    /**
     * 手机验证码
     * @phoneNumber phoneNumber手机号
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("sendMsg/{phoneNumber}")
    public void sendMsg(@PathVariable("phoneNumber") String phoneNumber , HttpServletRequest request,HttpServletResponse response) throws IOException {
//手机收到的验证码
        PrintWriter out = response.getWriter();
        //String code = MobileMessageSend.sendMsg(phoneNumber);
        String code = "1234";
        request.getSession().setAttribute("code",code);
        request.getSession().setMaxInactiveInterval(60*2);
        if(code!=null){
            code_a = (String) request.getSession().getAttribute("code");
            System.out.println(request.getSession().getAttribute("code"));
            System.out.println("验证码发送成功");
        }else{
            System.out.println("验证码发送失败");
        }
        out.flush();
        out.close();
    }

    @RequestMapping("addRegister/{rName}/{verificationCode}/{rpwd}/{phoneNumber}")
    public int addRegister(@PathVariable("rName") String rName , @PathVariable("verificationCode") String verificationCode , @PathVariable("rpwd") String rpwd , @PathVariable("phoneNumber") String phoneNumber,HttpServletRequest request){
        if (verificationCode.equals(code_a)){
            String Pwd = SecurityUtils.sha256Hex(rpwd);
            int cout = register_service.addRegister(rName,Pwd,phoneNumber);
            return cout;
        }else {
            return 0;
        }
    }



}
